// index.js
// 获取应用实例
var app = getApp()
import request from '../../utills/request'
// 路由守卫
import { routerFillter } from "../../utills/router";
routerFillter({

  data: {
    bannerList: [],//轮播图列表
    carInfo: { "pic": "车辆示意图.jpg", "license": "津BXXXX" },//车辆信息
    navId: "recommend",//导航标识
    newProList: [],//最优推荐列表
    projectLists2: [],//销量排序列表
    shoppingContent: [],//购物车内容
    sum: 0,//总价
    showPage: false,//购物车弹窗标识
  },


  //唤出购物车详情
  tap() {
    // console.log('click tap');
    let showPage = !this.data.showPage;
    this.setData({
      showPage
    });
  },
  clickoverlay() {
    this.setData({
      showPage: false
    });
  },

  //获取商品列表
  async getProjectList(navId) {
    //向后端发请求，获取商品列表，利用map()函数将数据新增quantity属性
    let newProListData = await request('/product/hot_list', 'GET');
    // console.log(newProListData.data)
    let newProList1 = newProListData.data
    var newProList = [];
    newProList1.map(((item, index) => {
      newProList.push(Object.assign({}, item, { quantity: 0 }))
    }))
    console.log(newProList, 0)
    this.setData({
      newProList: newProList
    })
  },

  // 切换导航栏
  changeNav(e) {
    // console.log(this.data.navId,this.data.navId=="recommend");
    let navId = e.currentTarget.id;
    // console.log(navId);
    this.setData({
      navId
    })
    this.getProjectList(this.data.navId);
    // console.log(this.data.navId)
  },

  // 数量减一
  minus(e) {
    let id = e.target.dataset.id
    var index = this.data.newProList.findIndex((item) => { return item.id == id })
    let arrObj = this.data.newProList.find((item) => { return item.id == id })
    arrObj.quantity--;
    // console.log(arrObj)
    let newProList = this.data.newProList;
    newProList.splice(index, 1, arrObj)
    this.setData({
      newProList
    });
    //根据商品数量删除购物车的商品
    console.log(this.data.shoppingContent.find((item) => { return item.pid == id }))
    if (this.data.shoppingContent.find((item) => { return item.pid == id })) {
      if (this.data.shoppingContent.find((item) => { return item.pid == id }).num > 1) {
        var index = this.data.shoppingContent.findIndex((item) => { return item.pid == id })
        let arrObj = this.data.shoppingContent.find((item) => { return item.pid == id })
        arrObj.num--;
        // console.log(arrObj,0)
        let shoppingContent = this.data.shoppingContent;
        shoppingContent.splice(index, 1, arrObj)
        this.setData({
          shoppingContent
        });
        // console.log(shoppingContent,1)
      }
      else {
        let shoppingContent = this.data.shoppingContent;
        console.log(this.data.shoppingContent.findIndex((item) => { return item.pid == id }))
        shoppingContent.splice(this.data.shoppingContent.findIndex((item) => { return item.pid == id }), 1);
        this.setData({
          shoppingContent
        })
        // console.log(shoppingContent,2)
      }
    }
    //根据购物车计算总价
    let sum = this.data.sum;
    sum = sum - arrObj.price;
    this.setData({
      sum
    })
    console.log(this.data.shoppingContent)
  },

  // 数量加一
  add(e) {
    let id = e.target.dataset.id
    var index = this.data.newProList.findIndex((item) => { return item.id == id })
    let arrObj = this.data.newProList.find((item) => { return item.id == id })
    // console.log(index,arrObj)
    arrObj.quantity++;
    // console.log(arrObj)
    let newProList = this.data.newProList;
    newProList.splice(index, 1, arrObj)
    this.setData({
      newProList
    });
    //根据商品数量加入购物车
    if (this.data.newProList[index].quantity) {
      // console.log(this.data.shoppingContent)
      if (this.data.shoppingContent.find((item) => { return item.pid == id })) {
        var index = this.data.shoppingContent.findIndex((item) => { return item.pid == id })
        let arrObj = this.data.shoppingContent.find((item) => { return item.pid == id })
        arrObj.num++;
        // console.log(arrObj)
        let shoppingContent = this.data.shoppingContent;
        shoppingContent.splice(index, 1, arrObj)
        this.setData({
          shoppingContent
        });
        // console.log(this.data.shoppingContent,1)
      }
      else {
        let shoppingContent = this.data.shoppingContent;
        let arrObj = { pid: '', num: '' };
        arrObj.pid = this.data.newProList[index].id; arrObj.num = this.data.newProList[index].quantity;
        shoppingContent.push(arrObj);
        this.setData({
          shoppingContent
        })
        // console.log(this.data.shoppingContent,2)
        // console.log(arrObj,shoppingContent)
      }
    }

    //根据购物车计算总价
    let sum = this.data.sum;
    sum = sum + arrObj.price
    this.setData({
      sum
    })
    console.log(this.data.shoppingContent)
  },

  //清空购物车
  clear() {
    let shoppingContent = [];
    let newProList = this.data.newProList.map(function (obj) {
      obj.quantity = 0;
      return obj;
    })
    this.setData({
      shoppingContent,
      newProList,
      sum: 0
    })
  },

  //支付并将购物车内容提交至后端
  toPay() {
    console.log(this.data.shoppingContent)
    console.log(JSON.stringify(this.data.shoppingContent))
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (optiond) {
    //获取首页广告轮播图
    if (app.globalData.token) {
      let bannersListData = await request('/ads/get_ads', 'POST')
      console.log(bannersListData, 1)
      this.setData({
        bannerList: bannersListData.data
      })
      this.getProjectList();
    }
    //获取车辆信息
    // let carInfoData = await request('/car/get_car',{license:"津AMA341"},'POST');
    // let carInfo = carInfoData.data
    // console.log(carInfoData)
    // this.setData({
    //   carInfo
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
